//
//  UITabBarController+x.m
//  Pods
//
//  Created by baboy on 1/21/16.
//
//

#import "UITabBarController+x.h"
#import "BKNavigationController.h"
#import "FileCache.h"
#import "Theme.h"

@implementation UITabBarController(x)
- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = THEME_VIEW_BG_COLOR;
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       THEME_TAB_BAR_TITLE_HL_COLOR, NSForegroundColorAttributeName,
                                                       THEME_TAB_BAR_TITLE_HL_FONT, NSFontAttributeName,
                                                       
                                                       nil] forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       THEME_TAB_BAR_TITLE_COLOR, NSForegroundColorAttributeName,
                                                       THEME_TAB_BAR_TITLE_FONT, NSFontAttributeName,
                                                       
                                                       nil] forState:UIControlStateNormal];
}
- (UIViewController *)createViewController:(Class )class withTitle:(NSString *)title{
    if(!class){
        return nil;
    }
    UIViewController *vc = [[class alloc] init];
    [vc setTitle:title];
    BKNavigationController *nav = [[BKNavigationController alloc] initWithRootViewController:vc];
    return nav;
}
- (NSArray *) loadViewControllers:(NSArray *)menus{
//    NSArray *menus = [NSArray arrayWithContentsOfFile:getBundleFilePath(plist, nil)];
    NSMutableArray *vcs = [NSMutableArray array];
    for (NSInteger i=0, n = menus.count; i < n; i++) {
        NSDictionary *item = [menus objectAtIndex:i];
        BOOL enable = [[item objectForKey:@"enable"] boolValue];
        if(!enable)
            continue;
        NSString *title = [item valueForKey:@"title"];
        
        NSString *icon = [item valueForKey:@"icon"];
        NSString *iconSelected = [item valueForKey:@"icon-hight-light"];
        if (!iconSelected)
            iconSelected = icon;
        Class c = NSClassFromString([item valueForKey:@"controller"]);
        if(!c)
            continue;
        
        UIViewController *vc = [self createViewController:c withTitle:title];
        vc.tabBarItem.image = [[UIImage imageNamed:icon] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.tabBarItem.selectedImage = [[UIImage imageNamed:iconSelected] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        //nav.tabBarItem.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);
        vc.tabBarItem.title = title;
        [vcs addObject:vc];
    }
    return vcs;
}
@end
